package model

import (
	"stock/global"
	"sync"

	"gorm.io/gorm"
)

type PurchaseOrderArticleModel struct {
	Base
}

type MallPurchaseOrderArticle struct {
	Id                       uint   `json:"id" gorm:"<-:false; primaryKey;column:id;type:int(11)"`
	MerchantId               uint   `json:"merchant_id" gorm:"<-:false; column:merchant_id;"`
	UserId                   uint   `json:"user_id" gorm:"column:user_id;"`
	ArticleType              string `json:"article_type" gorm:"column:article_type;"`
	Cover                    string `json:"cover" gorm:"column:cover;"`
	Path                     string `json:"path" gorm:"column:path;"`
	CreateTime               uint   `json:"create_time" gorm:"column:create_time;"`
	CheckState               uint8  `json:"check_state" gorm:"column:check_state;"`
	Status                   uint8  `json:"status" gorm:"column:status;"`
	IsRecommend              uint8  `json:"is_recommend" gorm:"column:is_recommend;"`
	Perfect                  string `json:"perfect" gorm:"column:perfect;"`
	PurchaseOrderArticleLang string `json:"lang" gorm:"column:lang;"`
	MallLang
}

type RequestMallPurchaseOrderArticle struct {
	Id          uint
	MerchantId  uint
	ArticleType string
	CheckState  uint8
	Status      uint8
	IsRecommend uint8
	Perfect     []string
	TableId     uint
	IdArr       []uint
}

type ResponsePurchaseOrderArticle struct {
	MallPurchaseOrderArticle
	ResponseLang
}

func (m *PurchaseOrderArticleModel) TabName() string {
	return global.CONF.Mysql.Prefix + "mall_purchase_order_article"
}

// 列表
func (m *PurchaseOrderArticleModel) List(param *RequestMallPurchaseOrderArticle, page global.PageSort) ([]ResponsePurchaseOrderArticle, error) {
	where := &MallPurchaseOrderArticle{}
	if param.Id > 0 {
		where.Id = param.Id
	}
	if param.MerchantId != 0 {
		where.MerchantId = param.MerchantId
	}
	if param.ArticleType != "" {
		where.ArticleType = param.ArticleType
	}
	if param.CheckState != 0 {
		where.CheckState = param.CheckState
	}
	if param.Status != 0 {
		where.Status = param.Status
	}
	var purOrderArti []MallPurchaseOrderArticle
	var purOrderArtArr []ResponsePurchaseOrderArticle
	var tx *gorm.DB
	tx = global.DB.Debug().Where(where)
	if len(param.IdArr) > 0 && len(param.Perfect) > 0 {
		tx = tx.Where("id IN ?", param.IdArr).Where("perfect IN ?", param.Perfect)
	} else if len(param.IdArr) > 0 {
		tx = tx.Where("id IN ?", param.IdArr)
	} else if len(param.Perfect) > 0 {
		tx = tx.Where("perfect IN ?", param.Perfect)
	}
	tx.Order(page.Sortfield + " " + page.Sort).Offset(page.Page).Limit(page.Limit).Find(&purOrderArti)
	if tx.Error != nil {
		return nil, tx.Error
	}
	purchaseOrderArticleLen := len(purOrderArti)
	if purchaseOrderArticleLen > 0 {
		var pLangM *LangModel = new(LangModel)
		var purchaseOrderArticleWg sync.WaitGroup
		purchaseOrderArticleWg.Add(purchaseOrderArticleLen)
		for i := 0; i < purchaseOrderArticleLen; i++ {

			var articlWg sync.WaitGroup
			articlWg.Add(purchaseOrderArticleLen)
			go m.manyTabData(m.TabName(), purOrderArti[i], pLangM, &purOrderArtArr, &articlWg)
			articlWg.Wait()
		}
		purchaseOrderArticleWg.Wait()
	}
	return purOrderArtArr, nil
}

// 基础详情
func (m *PurchaseOrderArticleModel) BaseDetail() {

}

// 详情
func (m *PurchaseOrderArticleModel) Detail() {

}

func (m *PurchaseOrderArticleModel) manyTabData(tabName string, articl MallPurchaseOrderArticle, pLangM *LangModel, ArticlArr *[]ResponsePurchaseOrderArticle, wg *sync.WaitGroup) {
	var resPurOrderArt ResponsePurchaseOrderArticle
	var purOrderArtL ResponseLang
	purOrderArtL, _ = pLangM.List(RequestLang{
		TableName: tabName,
		TableId:   articl.Id,
		Lang:      global.LANG_COUNTRY.Lang,
		Field:     []string{"introduction", "news_article_content", "title"},
	})
	resPurOrderArt.MerchantId = articl.MerchantId
	resPurOrderArt.Introduction = purOrderArtL.Introduction
	resPurOrderArt.Title = purOrderArtL.Title
	resPurOrderArt.Cover = articl.Cover
	resPurOrderArt.NewsArticleContent = purOrderArtL.NewsArticleContent
	resPurOrderArt.Content = purOrderArtL.Content
	*ArticlArr = append(*ArticlArr, resPurOrderArt)
	wg.Done()
}
